<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="姓名" prop="fullName">
                <el-input v-model="ruleForm.fullName"></el-input>
            </el-form-item>            
            <el-form-item label="用户名" prop="userName">
                <el-input v-model="ruleForm.userName"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="ruleForm.password" show-password></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="confirmPassword">
                <el-input v-model="ruleForm.confirmPassword" show-password></el-input>
            </el-form-item>
            <el-form-item label="头像" prop="headerImage">
                <el-upload class="avatar-uploader" :action="action" name="formFile" :show-file-list="false"
                    :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import { CheckUserName, Create } from '../../api/admin';
    export default {
        name: 'RbacUiAdminAdd',

        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            var validateUserName = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入用户名'));
                } else {
                    CheckUserName({ userName: value }).then(m => {
                        if (m.data) {
                            callback(new Error('用户名已被使用'));
                        }
                        else {
                            callback();
                        }
                    });
                }
            };
            return {
                ruleForm: {
                    fullName: "",
                    userName: "",
                    password: "",
                    confirmPassword: "",
                    headerImage: ""
                },
                rules: {
                    fullName: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ],
                    userName: [
                        { validator: validateUserName, trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ],
                    confirmPassword: [
                        { validator: validatePass, trigger: 'blur' }
                    ]
                },
                imageUrl: '',
                action: "https://localhost:5001/Upload/UploadFile"
            };
        },

        mounted() {

        },

        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        Create(this.ruleForm).then(m => {
                            let res = m.data;
                            if (res.code == 0) {
                                this.$message({
                                    message: res.msg,
                                    type: 'warning'
                                });
                            } else {
                                let _this = this;
                                this.$message({
                                    message: '添加成功',
                                    type: 'success',
                                    onClose: function (msg) {
                                        _this.$emit('OnSave', res, 'add');
                                    }
                                });
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            handleAvatarSuccess(res, file) {
                this.imageUrl = URL.createObjectURL(file.raw);
                this.ruleForm.headerImage = res.filePath;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }
        },
    };
</script>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>